package com.wandousoushu.jiusen.web.http;

import com.google.gson.Gson;
import com.wandousoushu.jiusen.MyApplication;
import com.wandousoushu.jiusen.bean.CodeBean;
import com.wandousoushu.jiusen.bean.LoginBean;
import com.wandousoushu.jiusen.bean.RegisterBean;
import com.wandousoushu.jiusen.booksource.data.entities.Book;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Random;

public class MainHttpUtils {
    private static MainHttpUtils instance = new MainHttpUtils();

    public static MainHttpUtils getInstance() {
        return instance;
    }

    public String Getcode(String mobile) {
        try {
            String postData = "mobile=" + URLEncoder.encode(mobile, "UTF-8");
            return HttpPostUtils.HttpPost(MainHttpConsts.Code, postData);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String Register(String mobile, String pass, String user_nickname, String code) {
        try {
            String postData = "mobile=" + URLEncoder.encode(mobile, "UTF-8") +
                    "&pass=" + URLEncoder.encode(pass, "UTF-8") +
                    "&user_nickname=" + URLEncoder.encode(user_nickname, "UTF-8") +
                    "&code=" + URLEncoder.encode(code, "UTF-8");
            return HttpPostUtils.HttpPost(MainHttpConsts.Register, postData);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String Login(String mobile, String pass) {
        try {
            String postData = "mobile=" + URLEncoder.encode(mobile, "UTF-8") +
                    "&pass=" + URLEncoder.encode(pass, "UTF-8");
            return HttpPostUtils.HttpPost(MainHttpConsts.Login, postData);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String Retrieve(String mobile, String pass, String code) {
        try {
            String postData = "mobile=" + URLEncoder.encode(mobile, "UTF-8") +
                    "&pass=" + URLEncoder.encode(pass, "UTF-8") +
                    "&code=" + URLEncoder.encode(code, "UTF-8");
            return HttpPostUtils.HttpPost(MainHttpConsts.Retrieve, postData);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String FeedBackCommit(String content, String token) {
        try {
            String postData = "content=" + URLEncoder.encode(content, "UTF-8");
            return HttpPostUtils.HttpPost(MainHttpConsts.FeedbackCommit, postData, token);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String PasswordChange(String oldPwd, String newPwd, String token) {
        try {
            String postData = "old_pass=" + URLEncoder.encode(oldPwd, "UTF-8") +
                    "&pass=" + URLEncoder.encode(newPwd, "UTF-8");
            return HttpPostUtils.HttpPost(MainHttpConsts.PasswordChange, postData, token);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String NameChangeSave(String name, String token) {
        try {
            String postData = "user_nickname=" + URLEncoder.encode(name, "UTF-8");
            return HttpPostUtils.HttpPost(MainHttpConsts.NameChange, postData, token);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String GetBookMail() {
        try {
            return HttpPostUtils.HttpPost(MainHttpConsts.GetBookMail, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String GetBookMailAd() {
        try {
            return HttpPostUtils.HttpPost(MainHttpConsts.GetBookMailAd, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String GetPageViewAd() {
        try {
            return HttpPostUtils.HttpPost(MainHttpConsts.GetPageViewAd, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String GetPageViewAdH5() {
        try {
            return HttpPostUtils.HttpPost(MainHttpConsts.GetPageViewAdH5, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String LoginOut(String token) {
        try {
            return HttpPostUtils.HttpPost(MainHttpConsts.LoginOut, "", token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String GetBookSource() {
        try {
            return HttpPostUtils.HttpPost(MainHttpConsts.GetBookSource, "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String AddSearchKey(String name, String token) {
        try {
            String postData = "name=" + URLEncoder.encode(name, "UTF-8");
            return HttpPostUtils.HttpPost(MainHttpConsts.AddSearckKey, postData, token);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String GetSearchKeys(String token) {
        try {
            return HttpPostUtils.HttpPost(MainHttpConsts.GetSearckKeys, "", token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String SetBook(Book book, int type, String token) {
        try {
            String coverUrl = "";
            if (book.getCoverUrl() != null) {
                coverUrl = book.getCoverUrl();
            }
            String postData = "type=" + type +
                    "&name=" + URLEncoder.encode(book.getName(), "UTF-8") +
                    "&url=" + URLEncoder.encode(book.getBookUrl(), "UTF-8") +
                    "&chapter_url=" + URLEncoder.encode(book.getTocUrl(), "UTF-8") +
                    "&final_refresh_data=" + URLEncoder.encode(book.getLatestChapterTitle(), "UTF-8") +
                    "&cover_url=" + URLEncoder.encode(coverUrl, "UTF-8") +
                    "&author=" + URLEncoder.encode(book.getAuthor(), "UTF-8") +
                    "&introduce=" + URLEncoder.encode(book.getIntro(), "UTF-8") +
                    "&origin=" + URLEncoder.encode(book.getOrigin(), "UTF-8") +
                    "&origin_name=" + URLEncoder.encode(book.getOriginName(), "UTF-8");
            return HttpPostUtils.HttpPost(MainHttpConsts.SetBook, postData, token);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String GetBooks(int type, String token) {
        try {
            String postData = "type=" + type;
            return HttpPostUtils.HttpPost(MainHttpConsts.GetBooks, postData, token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String RemoveSearchKeys(String token) {
        try {
            return HttpPostUtils.HttpPost(MainHttpConsts.RemoveSearckKeys, "", token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String RemoveSearchKey(String name, String token) {
        try {
            String postData = "name=" + URLEncoder.encode(name, "UTF-8");
            return HttpPostUtils.HttpPost(MainHttpConsts.RemoveSearckKeys, postData, token);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String GetPayRule(String token) {
        try {
            return HttpPostUtils.HttpPost(MainHttpConsts.GetPayRule, "", token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String GetUserInfo(String token) {
        try {
            return HttpPostUtils.HttpPost(MainHttpConsts.GetUserInfo, "", token);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String AddReadRecord(Book book, int type, String token) {
        try {
            String coverUrl = "";
            if (book.getCoverUrl() != null) {
                coverUrl = book.getCoverUrl();
            }
            String postData = "type=" + type +
                    "&name=" + URLEncoder.encode(book.getName(), "UTF-8") +
                    "&url=" + URLEncoder.encode(book.getBookUrl(), "UTF-8") +
                    "&chapter_url=" + URLEncoder.encode(book.getTocUrl(), "UTF-8") +
                    "&final_refresh_data=" + URLEncoder.encode(book.getLatestChapterTitle(), "UTF-8") +
                    "&cover_url=" + URLEncoder.encode(coverUrl, "UTF-8") +
                    "&author=" + URLEncoder.encode(book.getAuthor(), "UTF-8") +
                    "&introduce=" + URLEncoder.encode(book.getIntro(), "UTF-8") +
                    "&origin=" + URLEncoder.encode(book.getOrigin(), "UTF-8") +
                    "&origin_name=" + URLEncoder.encode(book.getOriginName(), "UTF-8") +
                    "&dur_chapter_title=" + URLEncoder.encode(book.getDurChapterTitle(), "UTF-8") +
                    "&dur_chapter_index=" + book.getDurChapterIndex() +
                    "&dur_chapter_pos=" + book.getDurChapterPos();
            return HttpPostUtils.HttpPost(MainHttpConsts.AddReadRecord, postData, token);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 换源
     *
     */
    public String UpdateOrigin(Book oldbook,Book newbook, int type, String token) {
        try {
            String coverUrl = "";
            if (newbook.getCoverUrl() != null) {
                coverUrl = newbook.getCoverUrl();
            }
            String postData = "type=" + type +
                    "&name=" + URLEncoder.encode(oldbook.getName(), "UTF-8") +
                    "&url=" + URLEncoder.encode(oldbook.getBookUrl(), "UTF-8") +
                    "&new_name=" + URLEncoder.encode(newbook.getName(), "UTF-8") +
                    "&new_url=" + URLEncoder.encode(newbook.getBookUrl(), "UTF-8") +
                    "&new_chapter_url=" + URLEncoder.encode(newbook.getTocUrl(), "UTF-8") +
                    "&new_final_refresh_data=" + URLEncoder.encode(newbook.getLatestChapterTitle(), "UTF-8") +
                    "&new_cover_url=" + URLEncoder.encode(coverUrl, "UTF-8") +
                    "&new_author=" + URLEncoder.encode(newbook.getAuthor(), "UTF-8") +
                    "&new_introduce=" + URLEncoder.encode(newbook.getIntro(), "UTF-8") +
                    "&new_origin=" + URLEncoder.encode(newbook.getOrigin(), "UTF-8") +
                    "&new_origin_name=" + URLEncoder.encode(newbook.getOriginName(), "UTF-8") +
                    "&new_dur_chapter_title=" + URLEncoder.encode(newbook.getDurChapterTitle(), "UTF-8") +
                    "&new_dur_chapter_index=" + newbook.getDurChapterIndex() +
                    "&new_dur_chapter_pos=" + newbook.getDurChapterPos();
            return HttpPostUtils.HttpPost(MainHttpConsts.ChangeOrigin, postData, token);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String WeiXinLogin(String openid, String code, String state) {
        try {

            String postData = "openid=" + URLEncoder.encode(openid, "UTF-8") +
                    "&code=" + URLEncoder.encode(code, "UTF-8") +
                    "&wx_state=" + URLEncoder.encode(state, "UTF-8");
            return HttpPostUtils.HttpPost(MainHttpConsts.WXLogin, postData);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

}
